package com.ruoyi.travel.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.travel.mapper.TravelConsultMapper;
import com.ruoyi.travel.domain.TravelConsult;
import com.ruoyi.travel.service.ITravelConsultService;

@Service
public class TravelConsultServiceImpl implements ITravelConsultService
{
    @Autowired
    private TravelConsultMapper travelConsultMapper;

    /**
     * 查询咨询管理
     *
     * @param consultId 咨询管理主键
     * @return 咨询管理
     */
    @Override
    public TravelConsult selectTravelConsultByConsultId(Long consultId)
    {
        return travelConsultMapper.selectTravelConsultByConsultId(consultId);
    }

    /**
     * 查询咨询管理列表
     *
     * @param travelConsult 咨询管理
     * @return 咨询管理
     */
    @Override
    public List<TravelConsult> selectTravelConsultList(TravelConsult travelConsult)
    {
//        travelConsult.setUserId(SecurityUtils.getUserId());
//        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
//            travelConsult.setUserRoleName("admin");
//        } else if (SecurityUtils.hasRole("地陪")) {
//            travelConsult.setUserRoleName("地陪");
//        } else if (SecurityUtils.hasRole("定制师")) {
//            travelConsult.setUserRoleName("定制师");
//        } else if (SecurityUtils.hasRole("客服")) {
//            travelConsult.setUserRoleName("客服");
//        }
        return travelConsultMapper.selectTravelConsultList(travelConsult);
    }

    /**
     * 新增咨询管理
     *
     * @param travelConsult 咨询管理
     * @return 结果
     */
    @Override
    public int insertTravelConsult(TravelConsult travelConsult)
    {

        // 生成 consultNumber：zx + 时间戳 (毫秒)+ 四位随机数
        StringBuilder consultNumber = new StringBuilder ();
        consultNumber.append ("zx"); // 固定前缀
        consultNumber.append (System.currentTimeMillis ()); // 毫秒级时间戳
// 生成四位随机数（0000-9999）
        Random random = new Random ();
        int randomNum = random.nextInt (10000);
        consultNumber.append (String.format ("%04d", randomNum)); // 不足四位补 0
// 设置生成的编号到实体类
        travelConsult.setConsultNumber (consultNumber.toString ());
        travelConsult.setCreateTime(DateUtils.getNowDate());
        travelConsult.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        travelConsult.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return travelConsultMapper.insertTravelConsult(travelConsult);
    }

    /**
     * 修改咨询管理
     *
     * @param travelConsult 咨询管理
     * @return 结果
     */
    @Override
    public int updateTravelConsult(TravelConsult travelConsult)
    {
        travelConsult.setUpdateTime(DateUtils.getNowDate());
        travelConsult.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return travelConsultMapper.updateTravelConsult(travelConsult);
    }

    /**
     * 批量删除咨询管理
     *
     * @param consultIds 需要删除的咨询管理主键
     * @return 结果
     */
    @Override
    public int deleteTravelConsultByConsultIds(Long[] consultIds)
    {
        return travelConsultMapper.deleteTravelConsultByConsultIds(consultIds);
    }

    /**
     * 删除咨询管理信息
     *
     * @param consultId 咨询管理主键
     * @return 结果
     */
    @Override
    public int deleteTravelConsultByConsultId(Long consultId)
    {
        return travelConsultMapper.deleteTravelConsultByConsultId(consultId);
    }




    @Override
    public int assignDesigner(List<Long> consultIds, Long designerId) {
        // 参数校验
        if (consultIds == null || consultIds.isEmpty()) {
            throw new IllegalArgumentException("咨询记录ID列表不能为空");
        }
        if(designerId==-1){
            designerId=null;
        }
        // 调用Mapper方法执行更新
        return travelConsultMapper.assignDesigner(consultIds, designerId);
    }

    @Override
    public int designerName(List<Long> consultIds, Long designerId) {
        return travelConsultMapper.designerName(consultIds, designerId);
    }
}
